<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

/**
 * 保障对象统计报表管理
 * Class ReportObjController
 * @package Admin\Controller
 */
class ReportObjController extends BaseController {

    static $mapping = array(
        "submit" => "待资格审查",
        "accept" => "待联合初审",
        "pass1" => "待房产局复审",
        "pass2" => "待房产局审批",
        "pass3" => "待资格公示",
        "pass4" => "待保障分配",
        "done" => "申请成功",
        "refuse" => "保障退出",
        "failed" => "申请失败",
    );

    protected function fn_GetLastYears($num=5){
        $years = array();
        $year = intval(date("Y"));
        for($i=-1; $i<$num; $i++){
            $years[] = $year-$i;
        }
        return $years;
    }

    public function ajax_export(){

        header("Content-type: text/html; charset=utf-8");
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=保障申请统计报表.xls");  //文件下载

        $apply = array();
        $total = array();
        $year = I("request.year")?I("request.year"):"all";
        $this->action_GenerateReport($year, $apply, $total);

        echo '<table width="200" border="1">
                  <tr>
                    <td>申请状态</td>
                    <td>廉租房申请数量</td>
                    <td>公租房申请数量</td>
                    <td>租赁补贴申请数量</td>
                  </tr>';
        foreach(array_keys(self::$mapping) as $item){
            $tr = "
                <tr>
                <td>{{name}}</td>
                <td>{{lzf}}</td>
                <td>{{gzf}}</td>
                <td>{{zlbt}}</td>
              </tr>";
            echo  strtr($tr, [
                "{{name}}" => self::$mapping[$item],
                "{{lzf}}" => $apply['lzf'][$item]['num'],
                "{{gzf}}" => $apply['gzf'][$item]['num'],
                "{{zlbt}}" => $apply['zlbt'][$item]['num'],
            ]);
        }
        $footer = "
                <tr>
                <td>总计：</td>
                <td>{{lzfTotal}}</td>
                <td>{{gzfTotal}}</td>
                <td>{{zlbtTotal}}</td>
              </tr>
            </table>";
        echo  strtr($footer, ["{{lzfTotal}}" => $total['lzf'],"{{gzfTotal}}" => $total['gzf'],"{{zlbtTotal}}" => $total['zlbt']]);
    }


    private function action_GenerateReport($year, &$apply, &$total){
        $where1 = array();
        $where2 = array();
        $where3 = array();
        if(I("post.year")!="all"){
            $year1 = strtotime($year."/01/01 00:00:00");
            $year2 = strtotime($year."/12/31 24:00:00");
            $where1["l.stime"] = array('between',array($year1, $year2));
            $where2["g.stime"] = array('between',array($year1, $year2));
            $where3["z.stime"] = array('between',array($year1, $year2));
        }

        $lzfData = M("lzf l")->field("state, count(id) as num")->where($where1)->group("state")->select();
        foreach($lzfData as $key => $value){
            $where1['l.state'] = $value['state'];
            $lzfData[$key]['where'] = serialize($where1);
        }
        $gzfData = M("gzf g")->field("state, count(id) as num")->where($where2)->group("state")->select();
        foreach($gzfData as $key => $value){
            $where2['g.state'] = $value['state'];
            $gzfData[$key]['where'] = serialize($where2);
        }
        $zlbtData = M("zlbt z")->field("state, count(id) as num")->where($where3)->group("state")->select();
        foreach($zlbtData as $key => $value){
            $where3['z.state'] = $value['state'];
            $zlbtData[$key]['where'] = serialize($where3);
        }
        $apply = array(
            "lzf" => $this->fn_ConverListCountToDict($lzfData),
            "gzf" => $this->fn_ConverListCountToDict($gzfData),
            "zlbt" => $this->fn_ConverListCountToDict($zlbtData),
        );
        $total = array(
            'lzf' => $this->fn_CalTotalApplies($lzfData),
            'gzf' => $this->fn_CalTotalApplies($gzfData),
            'zlbt' => $this->fn_CalTotalApplies($zlbtData),
        );
    }

    /**
     * 保障对象统计报表管理 -> formview
     */
    public function detail(){

        $years = $this->fn_GetLastYears();
        $this->assign("years", $years);
        $year = I("post.year")?I("post.year"):date("Y");
        $this->assign("year", $year);

        $apply = array();
        $total = array();
        $this->action_GenerateReport($year, $apply, $total);
        $this->assign("apply", $apply);
        $this->assign("total", $total);
        $this->assign("statusList", array_keys(self::$mapping));
        $this->assign("statusDict", self::$mapping);
        $this->display();
    }

    protected function fn_CalTotalApplies($data){
        $total = 0;
        foreach($data as $value){
            $total += $value['num'];
        }
        return $total;
    }

    protected function fn_ConverListCountToDict($list){
        $data = array(
            "submit" => array("state"=>"submit","num"=>0,"where"=>""),
            "accept" => array("state"=>"submit","num"=>0,"where"=>""),
            "pass1" => array("state"=>"submit","num"=>0,"where"=>""),
            "pass2" => array("state"=>"submit","num"=>0,"where"=>""),
            "pass3" => array("state"=>"submit","num"=>0,"where"=>""),
            "pass4" => array("state"=>"submit","num"=>0,"where"=>""),
            "done" => array("state"=>"submit","num"=>0,"where"=>""),
            "refuse" => array("state"=>"submit","num"=>0,"where"=>""),
            "failed" => array("state"=>"submit","num"=>0,"where"=>""),
        );
        foreach($list as $value){
            $data[$value['state']] = $value;
        }
        return $data;
    }

}